/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import com.informatica.powercenter.sdk.mapfwk.core.Folder;
import com.informatica.powercenter.sdk.mapfwk.core.MapFwkRetrieveContext;
import com.informatica.powercenter.sdk.mapfwk.core.Mapping;
import com.informatica.powercenter.sdk.mapfwk.core.Mapplet;
import com.informatica.powercenter.sdk.mapfwk.core.Source;
import com.informatica.powercenter.sdk.mapfwk.core.Target;
import com.informatica.powercenter.sdk.mapfwk.core.Transformation;
import com.informatica.powercenter.sdk.mapfwk.repository.Repository;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * 
 * @author Administrator
 */
public class local {
	static Logger Log = Logger.getLogger(local.class);
	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) {
		// TODO code application logic here
		// Directory path here
		if(args.length<1){
			System.out.println("Usage: chekfiles folder_containing_xmlfiles");
			System.exit(1);
		}
		
		String path = args[0];
		Repository rep=null;
		String files="";
		File folder = new File(path);
		File[] listOfFiles = folder.listFiles();

		for (int x = 0; x < listOfFiles.length; x++) {

			if (listOfFiles[x].isFile()) {
				try {
					files = listOfFiles[x].getName();
					if (files.endsWith(".xml") || files.endsWith(".XML")) {
						rep = new Repository();
						rep.retrieveRepo(new MapFwkRetrieveContext(
								MapFwkRetrieveContext.INPUT_FORMAT_XML,
								MapFwkRetrieveContext.INPUT_FORMAT_XML,
								listOfFiles[x].getAbsolutePath()));

						List<Folder> folders = rep.getAddedFolders();

						// gets folder count from repository
						int folderSize = folders.size();
						// System.out.println(folderSize);
						// System.out.println("List of folder present in repository: "
						// + rep.getRepoConnectionInfo().getTargetRepoName());
						// folder count - in this case it is always 1

						for (int i = 0; i < folderSize; i++) {
							List<Source> listOfSources = ((Folder) folders
									.get(i)).getSources(); // ge tthe list of
															// sources
							int listSize = listOfSources.size();
							// System.out.println(" ***** List of Sources ******");
							for (int j = 0; j < listSize; j++) {
								// System.out.println((listOfSources.get(j)).getName());
								(listOfSources.get(j)).getName();
							}
						}

						for (int i = 0; i < folderSize; i++) {
							List<Transformation> listOfTransformations = ((Folder) folders
									.get(i)).getTransformations();
							int listSize = listOfTransformations.size();
							// System.out.println(" ***** List of Reusable Transformations ******");
							for (int j = 0; j < listSize; j++) {
								// System.out.println((listOfTransformations.get(j)).getName());
								(listOfTransformations.get(j)).getName();
							}
						}

						for (int i = 0; i < folderSize; i++) {
							List<Target> listOfTargets = ((Folder) folders
									.get(i)).getTargets(); // get the list of
															// targets
							int listSize = listOfTargets.size();
							// System.out.println(" ***** List of Targets ******");
							for (int j = 0; j < listSize; j++) {
								// System.out.println((listOfTargets.get(j)).getName());
								(listOfTargets.get(j)).getName();
							}
						}

						for (int i = 0; i < folderSize; i++) {
							List<Mapplet> listOfMapplets = ((Folder) folders
									.get(i)).getMapplets(); // get the list of
															// mapplets
							int listSize = listOfMapplets.size();
							// System.out.println(" ***** List of Mapplets ******");
							for (int j = 0; j < listSize; j++) {
								// System.out.println((listOfMapplets.get(j)).getName());
								(listOfMapplets.get(j)).getName();
							}
						}

						for (int i = 0; i < folderSize; i++) {
							List<Mapping> listOfMappings = ((Folder) folders
									.get(i)).getMappings(); // get the list of
															// mappings
							int listSize = listOfMappings.size();
							// System.out.println(" ***** List of Mappings ******");
							for (int j = 0; j < listSize; j++) {
								// System.out.println((listOfMappings.get(j)).getName());
								(listOfMappings.get(j)).getName();
								List<Transformation> listOfTransformations = (listOfMappings
										.get(j)).getTransformations();

								// System.out.println(" List of Transformations ******");
								for (int k = 0; k < listOfTransformations
										.size(); k++) {
									// System.out.println("   "+(listOfTransformations.get(k)).getName());
									(listOfTransformations.get(k)).getName();
								}
							}
						}
						Log.info(files+" is successful");
						rep=null;
						System.gc();
					}
				} catch (Exception e) {
					rep=null;
					System.gc();
					Log.error(files+" has an error: "+e.getMessage());
				}
			}

		}
	}
}
